package com.yxj.securityhello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxj.securityhello.domain.Svsuser;
import com.yxj.securityhello.domain.vo.LoginUser;
import com.yxj.securityhello.mapper.SvsuserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author yangxuejiao
* @description 针对表【SVSUSer(用户表)】的数据库操作Service实现
* @createDate 2025-11-02 17:53:54
*/
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final SvsuserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.根据用户名查询账号信息
        QueryWrapper<Svsuser> query = new QueryWrapper<>();
        query.eq("user_name",username);
        Svsuser user = userMapper.selectOne(query);
        if(Objects.isNull(user)) {
            throw new RuntimeException("用户不存在");
        }
        //自动判断密码是否正确

        //3.返回UserDetails对象
        return new LoginUser(user);
    }
}




